package com.patentdb;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.depict.Depiction;
import org.openscience.cdk.depict.DepictionGenerator;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.exception.InvalidSmilesException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.isomorphism.Pattern;
import org.openscience.cdk.renderer.elements.IRenderingVisitor;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smarts.SmartsPattern;
import org.openscience.cdk.smiles.SmilesParser;

import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * @author rui
 * @ClassName: test
 * @create 2023-10-18 11:53
 * @description
 */
public class test {

    @Test
    public void test1(){
                try {
                    // Create a SMILES parser
                    SmilesParser smilesParser = new SmilesParser(SilentChemObjectBuilder.getInstance());

                    // Parse the SMILES string
                    IAtomContainer molecule = smilesParser.parseSmiles("CCO"); // Replace with your SMILES string

                    // Generate a 2D depiction
                    DepictionGenerator depictionGenerator = new DepictionGenerator();
                    depictionGenerator.withSize(300, 200); // Set image size
                    depictionGenerator.withBackgroundColor(Color.WHITE);

                    // Generate the SVG image

                    // Save the SVG to a file


                    System.out.println("SVG image saved to molecule.svg");
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }

    @Test
    public void test2() throws CDKException, IOException {
        IChemObjectBuilder bldr   = SilentChemObjectBuilder.getInstance();
        SmilesParser       smipar = new SmilesParser(bldr);

        IAtomContainer mol = smipar.parseSmiles("CC1=C(C2=C(O1)C=C(C=C2)OC3=C4C=CC(=CC4=NC=C3)OCCN5CCOCC5)C(=O)NC");
//        mol.setProperty(CDKConstants.TITLE, "caffeine"); // title already set from input!

        DepictionGenerator dptgen = new DepictionGenerator();
        dptgen.withSize(60, 60)              // px (raster) or mm (vector)
                .withMolTitle()
                .withTitleColor(Color.DARK_GRAY) // annotations are red by default
                .depict(mol)
                .writeTo("caffeine.svg");      // svg preferred but will be 20cmx25cm!

//        dptgen.depict(mol).writeTo("caffeine.svg");
//        BufferedImage bufferedImage = dptgen.depict(mol).toImg();

        BufferedImage bufferedImage = dptgen.depict(mol).toImg();
//        ImageIO.write(bufferedImage, "svg", baos);
//        String s = dptgen.depict(mol).toSvgStr();
        FileOutputStream fos = new FileOutputStream("test.svg");
//        InputStream inputStream = IOUtils.toInputStream(s);
//        IOUtils.copy(inputStream, fos);
//        System.out.println(s);


        String s = dptgen.depict(mol).toSvgStr();
//        BufferedImage bufferedImage = dptgen.depict(mol).toImg();
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        ImageIO.write(bufferedImage, "png", outputStream);
//        byte[] imageBytes = outputStream.toByteArray();
//        response.addHeader("Content-Disposition","attachment;filename=123.png");
//        response.addHeader("Content-Disposition","attachment;filename=123.svg");
        String base64String = Base64.getEncoder().encodeToString(s.getBytes());
//        os.write(s.getBytes());

    }

    @Test
    public void test3() throws CDKException, IOException {
        IChemObjectBuilder bldr   = SilentChemObjectBuilder.getInstance();
        SmilesParser       smipar = new SmilesParser(bldr);

        IAtomContainer mol = smipar.parseSmiles("CC1=C(C2=C(O1)C=C(C=C2)OC3=C4C=CC(=CC4=NC=C3)OCCN5CCOCC5)C(=O)NC");
        DepictionGenerator dptgen = new DepictionGenerator();
        dptgen.withSize(60, 60)
                .withAtomNumbers().withAnnotationColor(Color.BLACK).depict(mol).writeTo("1.svg");
    }

    @Test
    public void test4() throws Exception{

        String smiles1 = "C=C";
        String smiles2 = "C=C-C";
        IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
        SmilesParser sp = new SmilesParser(builder);
        IAtomContainer molecule1 = sp.parseSmiles(smiles1);
        IAtomContainer molecule2 = sp.parseSmiles(smiles2);
        Pattern pattern = SmartsPattern.findSubstructure(molecule2);
        boolean m = pattern.matches(molecule1);
        System.out.println(m);

    }
}
